/*
  elementTransitions.js
*/
var PageTransitions = (function($) {
  var startElement = 0,
  animEndEventNames = {
    'WebkitAnimation': 'webkitAnimationEnd',
    'OAnimation': 'oAnimationEnd',
    'msAnimation': 'MSAnimationEnd',
    'animation': 'animationend'
  }

  function getTransitionPrefix() {
    var b = document.body || document.documentElement;
    var s = b.style;
    var p = 'animation';
    if(typeof s[p] == 'string')
      return 'animation';

    // Tests for vendor specific prop
    v = ['Moz', 'Webkit', 'Khtml', 'O', 'ms'],
    p = p.charAt(0).toUpperCase() + p.substr(1);
    for( var i=0; i<v.length; i++ ) {
      if(typeof s[v[i] + p] == 'string')
        return v[i] + p;
    }
    return false;
  }
  // animation end event name
  animEndEventName = animEndEventNames[getTransitionPrefix()];

  function init() {
    animate(".et-rotate");
  }

  function initPage(page,callback) {
      animate(".et-rotate","",page);
      if(typeof callback==="function"){
          callback();
      }
  }

  function reset() {
    $(".et-page").each(function() {
      $(this).data('originalClassList', $(this).removeClass('et-page-current').attr('class'));
    });
    $(".et-wrapper").each(function() {
      $(this).data('current', 0);
      $(this).data('isAnimating', false);
      $(this).children(".et-page").eq(startElement).addClass('et-page-current');
    });

  }

  function animate(block, callback,page) {
    nextPage($(block), $(block).attr('et-out')||"show", $(block).attr('et-in')||"hide", callback,page);
  }

  function nextPage(block, outClass, inClass, callback,page) {
    //console.info(outClass,inClass);
    block = $(block);
    inClass = formatClass(inClass);
    outClass = formatClass(outClass);
    var current = block.data('current'),
        $pages = block.children('.et-page'),
        pagesCount = $pages.length,
        endCurrPage = false,
        endNextPage = false,
        $currPage;

    if(block.data('isAnimating')) {
      return false;
    }



    block.data('isAnimating', true);

    $currPage = $pages.eq(current);

    if(page){
      current = $('.et-page').index(page)
    }else{
      if(current < pagesCount - 1) {
        current++;
      }
      else {
        current = 0;
      }
    }


    block.data('current', current);

    var $nextPage = $pages.eq(current).addClass('et-page-current');



      $currPage.addClass(outClass).on(animEndEventName, function() {
          //alert(current);
      $currPage.off(animEndEventName);
      endCurrPage = true;

      if(endNextPage) {
        if(jQuery.isFunction(callback)) {
          callback(block, $nextPage, $currPage);
        }
        onEndAnimation($currPage, $nextPage, block);
      }
    });

    $nextPage.addClass(inClass).on(animEndEventName, function() {
      $nextPage.off(animEndEventName);
      endNextPage = true;
      if(endCurrPage) {
        onEndAnimation($currPage, $nextPage, block);
      }
    });

  }

  function onEndAnimation($outpage, $inpage, block) {
    resetPage($outpage, $inpage);
    block.data('isAnimating', false);
  }

  function resetPage($outpage, $inpage) {
    $outpage.attr('class', $outpage.data( 'originalClassList'));
    $inpage.attr('class', $inpage.data( 'originalClassList') + ' et-page-current');
  }

  function formatClass(str) {
      //console.info(str);
    classes = str.split(" ");
    output = "";
    for(var n=0; n<classes.length; n++){
      output += " pt-page-" + classes[n];
    }
    return output;
  }
  return {
    init : init,
    initPage : initPage,
    reset: reset,
    nextPage: nextPage,
    animate: animate
  };
})(jQuery);